package trucaso;

/*
 *  Main game Class, contains some action listeners and Stuff also.
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;


import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class Truco extends JFrame{
	MenuBar menu;
	Actions act;
	JSplitPane jsp;
	JPanel sidePane;
	CardZone cz;
	ScoreBoard pp;
	Deck deck;
	Game game;
	JFileChooser fc;
	FileFilter ff;
	ChatPane chatPane;
		
	public Truco(Dimension size) {
		
		//Creates the frame, sets the right size and adds a Border Layout. 
		super("Truco! O algo...");
		setSize(size);
		setLayout(new BorderLayout());
		
		//Creates the menu and adds it to the frame
		menu = new MenuBar(this);
		setJMenuBar(menu);
		
		
		chatPane = new ChatPane(null);
		getContentPane().add(chatPane, BorderLayout.SOUTH);
		
		//Creates a file chooser for the Change Background option
		fc = new JFileChooser();
		ff = new ImageFilter();
		fc.setFileFilter(ff);
		
		final GameFlowHandler flow = new GameFlowHandler(game);		
		
		//Creates an Actions panel (the one with the game options)
		act = new Actions(flow);
		
		//Creates the deck
		deck = new Deck();
		
		
		//Creates the CardZone and the ScoreBoard and adds it to a JSplit Pane
		//that's located at the center of the main frame.
		
		cz = new CardZone(new Dimension(450,500),deck,null);		
		pp = new ScoreBoard();
		
		sidePane = new JPanel();
		sidePane.setLayout(new BorderLayout());
		sidePane.add(pp,BorderLayout.CENTER);
		sidePane.add(act,BorderLayout.SOUTH);
		
		jsp = new JSplitPane();
		jsp.setLeftComponent(cz);
		jsp.setRightComponent(sidePane);
		jsp.setDividerLocation(600);
		
		// Listener for the splitPane (for when you move the divider)
		jsp.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent evt) {
				cz.newSize(jsp.getLeftComponent().getSize());

			}
		});
		

		getContentPane().add(jsp,BorderLayout.CENTER);
		
		
		//Sets the window visible and sets the close operation (X)
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Listens to window changes
		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				jsp.setDividerLocation(jsp.getSize().width
                        - jsp.getInsets().right
                        - jsp.getDividerSize()
                        - 200);
			}
		});
		
		
		//Listener for the menu's New Local Game Option.
		menu.localGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				game = new LocalClient(deck,cz,act,pp,chatPane);
				flow.setGame(game);
				flow.setChatPane(chatPane);
				flow.reset();
				cz.game = game;
				cz.setCards();
				act.resetButtons();
				cz.repaint();
			}
		});
		
		menu.create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					int port = Integer.parseInt(JOptionPane.showInputDialog(cz,"Please select a port","4444"));
					game = new Server(deck,cz,act,pp,chatPane,port);
				}catch(NumberFormatException nfex) {}
			}
		});
		
		menu.join.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					String s = JOptionPane.showInputDialog(cz,"Connect to:\nSyntax: address:port");
					s.trim();
					int i=0;
					while(s.charAt(i)!=':') {
						i++;
					}
					int port = Integer.parseInt(s.substring(i+1));
					String server = s.substring(0,i);
					game = new RemoteClient(deck,cz,act,pp,chatPane,server,port);
				}catch(NullPointerException npex) {}
			}
		});
		
		//Listener for the menu's Change Background option
		menu.background.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				int ret = fc.showOpenDialog(cz);
				if(ret == JFileChooser.APPROVE_OPTION) {
					 cz.setBackground(fc.getSelectedFile().toString());
				}
			}
		});
		
		menu.changeName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try{
					String name = JOptionPane.showInputDialog(cz,"New nickname:");
					if(name.length()>0) {
					if(name.length()>10){
						String n1 = name.substring(0,11);
						name = n1;
						}
						game.changeName(name);
					}
				}catch(NullPointerException npex) {}
			}
		});

	}
	
	public static void main(String[] args) {
		//The program runs on it's own Thread, I think...
		 javax.swing.SwingUtilities.invokeLater(new Runnable() {
		        public void run() {
		        	Truco t;
		        	t = new Truco(new Dimension(800,600));
		        	//Do one resize to size everything in the Card Zone properly
		        	//Otherwise the cards are not displayed correctly
		        	t.cz.newSize(t.jsp.getLeftComponent().getSize());
		        }
		 });

	}

}
